// This is for XP
#define WINVER 0x0501 

///////////////////////////////////////////////////////////////////////////////////////////////////Header
#include <afxwin.h>
#include <string.h>
#include "Winthread_Client.h"
#include "Winthread_Server.h"

///////////////////////////////////////////////////////////////////////////////////////////////////Variablr

extern char RecvBuffCli[1024];
extern char SendBuffCli[1024];
extern char RecvBuffSer[1024];
extern char SendBuffSer[1024];
extern int SockStatus;
int Select_SerCli=0;
int cnt=0;

///////////////////////////////////////////////////////////////////////////////////////////////////Class
class CApp:public CWinApp{
public:
	virtual BOOL InitInstance();
};

class CWin:public CFrameWnd{
	CButton *bt1;
	CButton *bt2;
	CFont   *ft;
	CEdit   *et;

public:
	CWin();
	~CWin();
	DECLARE_MESSAGE_MAP();
	afx_msg void OnClick1();
	afx_msg void OnClick2();
	afx_msg void OnTimer(UINT deleteThisVariablesIfNotNeeded);
};
///////////////////////////////////////////////////////////////////////////////////////////////////Message Map Loop
BEGIN_MESSAGE_MAP(CWin,CWnd)
	ON_BN_CLICKED(101,OnClick1)
	ON_BN_CLICKED(102,OnClick2)
	ON_WM_TIMER()
END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////////////////////////////Init Window
BOOL CApp::InitInstance(){
	m_pMainWnd= new CWin();
	m_pMainWnd->ShowWindow(m_nCmdShow);
	m_pMainWnd->UpdateWindow();
	return TRUE;
}

///////////////////////////////////////////////////////////////////////////////////////////////////Create resource in Window
CWin::CWin(){

	this->Create(NULL,"HProgram",WS_OVERLAPPEDWINDOW,CRect(100,100,550,500));
	this->SetTimer(1,10,NULL);

	ft=new CFont();
	ft->CreateFont(14,0,0,0,100,0,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH,"Times New Roman");

	bt1=new CButton();
	bt1->Create("Server",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(300,50,400,70),this,101);
	bt1->SetFont(ft);
	bt1->SetFocus();

	bt2=new CButton();
	bt2->Create("Client",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(300,70,400,90),this,102);
	bt2->SetFont(ft);
	bt2->SetFocus();

//	et=new CEdit();
//	et->Create(WS_CHILD|WS_VISIBLE|WS_BORDER,CRect(300,250,400,300),this,102);
//	et->SetFont(ft);
//	et->SetFocus();
}
///////////////////////////////////////////////////////////////////////////////////////////////////Delete resource
CWin::~CWin(){
	if (Select_SerCli==1) UnInitWinThread_S();
	if (Select_SerCli==2) UnInitWinThread_C();
	delete ft;
	delete bt1;
	delete bt2;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
CApp theapp;

///////////////////////////////////////////////////////////////////////////////////////////////////
void CWin::OnClick1(){
	if (Select_SerCli==0){
		InitWinThread_S();
		Select_SerCli=1;
		bt2->DestroyWindow();
	}
}
void CWin::OnClick2(){
	if (Select_SerCli==0){
		InitWinThread_C();
		Select_SerCli=2;
		bt1->DestroyWindow();
	}
}

void CWin::OnTimer(UINT deleteThisVariablesIfNotNeeded){
	CClientDC dc(this);
	if (Select_SerCli==1){
		if (SockStatus==0) dc.TextOut (1,10,"Activate ");
		if (SockStatus==1) dc.TextOut (1,10,"Listening");
		if (SockStatus==2) dc.TextOut (1,10,"Connected");
	}
	if (Select_SerCli==1) if (RecvBuffSer) dc.TextOut (100,100,RecvBuffSer);
	if (Select_SerCli==2) if (RecvBuffCli) dc.TextOut (100,120,RecvBuffCli);
}

//	char tom[24];
//	et->GetLine(1,tom);
//	CClientDC dc(this);
//	dc.TextOut (100,300,tom);
//	sprintf( SendBuffCli,"@ %s %d",tom, cnt++);
//	sprintf( SendBuffCli, "@Pholchai %d",cnt++);
